#include "fileOperationsHeader.h"
char *moveFile(char sourcePath[], char destpath[])
{
	if(mh==NULL)
		return ERR_VFS_MOVEFILE_06;
	nAryNode *temp=nAryRoot;
	char *tempDestDirPath=(char*)malloc(sizeof(char)*strlen(destpath)+1);
	sprintf(tempDestDirPath,"%s",destpath);
	int b=0;
	for(;tempDestDirPath[b]!='\0';b++)
	{
		if(tempDestDirPath[b]=='/')
			break;
	}
	if(b>=strlen(tempDestDirPath))
		return ERR_VFS_MOVEFILE_02;
	int i=strlen(tempDestDirPath)-1;
	for(;tempDestDirPath[i]!='/';i--);
	char *destParent=(char*)malloc(sizeof(char)*(i+1));
	int j=0;
	for(;j<i;j++)
		destParent[j]=tempDestDirPath[j];
	destParent[j]='\0';
	char *dest=(char*)malloc(sizeof(char)*(strlen(tempDestDirPath)-1-i+1));
	j=0;
	i++;
	for(;tempDestDirPath[i]!='\0';j++)
	{
		dest[j]=tempDestDirPath[i];
		i++;
	}
	dest[j]='\0';
	if(strcmp(destParent,"")==0 || strcmp(destParent," ")==0)
		destParent="/";
	char *destPathTokens=NULL;
	char *tempDestParent=(char*)malloc(strlen(destParent)+1);
	sprintf(tempDestParent,"%s",destParent);
	destPathTokens=strtok(destParent,"/");
	int found=0;
	while(destPathTokens!=NULL)
	{
		found=0;
		if(temp==nAryRoot)
		{
			if(temp->child!=NULL)
				temp=temp->child;
			else
				return ERR_VFS_MOVEFILE_02;
		}
		else
		{
			while(temp->sibling!=NULL)
			{
				if(strcmp(temp->name,destPathTokens)!=0)
					temp=temp->sibling;
				else
				{
					found=1;
					break;
				}
			}
			if(strcmp(temp->name,destPathTokens)==0)
				found=1;
			if(!found)
				return ERR_VFS_MOVEFILE_02;
			destPathTokens = strtok( NULL, "/" );
			if(destPathTokens!=NULL && temp->child==NULL)
				return ERR_VFS_MOVEFILE_02;
			if(temp->child!=NULL)
				temp=temp->child;
		}
	}
	FILE *fp=fopen(mh->fileSystemLabel,"r+b");
	char *desfilename,*desDirPath;
	desfilename=(char*)malloc(sizeof(char)*strlen(destpath));
	desDirPath=(char*)malloc(sizeof(char)*(strlen(destpath)+1));
	int currentFileDescriptors=mh->countOfFileDescriptors;
	int maxFileDescriptors=mh->maxDescriptors;
	int noOfBlocks=maxFileDescriptors/2;
	//for testing only, just in case...
	/*int q=0;
	while(q<maxFileDescriptors)
	{
		if(h->desc[q].type=='f' || h->desc[q].type=='d')
		{
			printf("Location %s\n",h->desc[q].location);
			printf("Filename %s\n",h->desc[q].fileName);
			printf("Type %c \n",h->desc[q].type);
		}
		q++;
	}*/
	i=0;
	j=0;
	descriptors *s=(descriptors*)malloc(sizeof(descriptors));
	s=(descriptors*)searchBST(sourcePath);
	if(s==NULL)
		return ERR_VFS_MOVEFILE_01;
	descriptors *destFD=(descriptors*)malloc(sizeof(descriptors));
	destFD=(descriptors*)searchBST(destpath);
	int iterDescriptors=0;
	int iterDesc=0;
	if(destFD==NULL)
	{
		iterDesc=0;
		while(iterDesc<maxFileDescriptors)
		{
			if(strcmp(h->desc[iterDesc].location,s->location)==0 && strcmp(h->desc[iterDesc].fileName,s->fileName)==0 && h->desc[iterDesc].type=='f')
				break;
			iterDesc++;
		}
		sprintf(s->location,"%s",tempDestParent);
		sprintf(s->fileName,"%s",dest);
	}
	else
	{
		iterDescriptors=0;
		while(iterDescriptors<maxFileDescriptors)
		{
			if(strcmp(h->desc[iterDescriptors].location,destFD->location)==0 && strcmp(h->desc[iterDescriptors].fileName,destFD->fileName)==0 && h->desc[iterDescriptors].type=='f')
				break;
			iterDescriptors++;
		}
		sprintf(destFD->fileName,"%s",dest);
		sprintf(destFD->location,"%s",tempDestParent);
		destFD->size=s->size;
		destFD->blockNumber=s->blockNumber;
		destFD->type='f';
		h->desc[iterDescriptors]=*destFD;
		iterDesc=0;
		while(iterDesc<maxFileDescriptors)
		{
			if(strcmp(h->desc[iterDesc].location,s->location)==0 && strcmp(h->desc[iterDesc].fileName,s->fileName)==0 && h->desc[iterDesc].type=='f')
				break;
			iterDesc++;
		}
		s->type=' ';
		s->size=-1;
		s->blockNumber=-1;
	}
	h->desc[iterDesc]=*s;
	fclose(fp);
	int bstRet=insertBSTWhileMount(maxFileDescriptors,h->desc);
	if(bstRet!=0)
		return "FAILURE <while creating BST>";
	int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
	if(nAryRet!=0)
		return "FAILURE <while creating nAryTree>";
	//traversenAryTree(nAryRoot);
	int hashTableRet=hashTableMethod(h->desc,maxFileDescriptors);
	if(hashTableRet!=0)
		return "FAILURE <while creating hashTable>";
	return "SUCCESS";
}
